package com.example.controller.admin;

import com.example.constant.JwtClaimsConstant;
import com.example.pojo.dto.EnrollCourseDto;
import com.example.pojo.dto.UserLoginDto;
import com.example.pojo.dto.UserPageQueryDto;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Course;
import com.example.pojo.entity.User;
import com.example.pojo.vo.SelectCourseVo;
import com.example.pojo.vo.UserInfoVo;
import com.example.pojo.vo.UserLoginVo;
import com.example.properties.JwtProperties;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.CourseService;
import com.example.service.UserService;
import com.example.service.impl.AdminService;
import com.example.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
@Api(tags = "登录相关接口")
@Slf4j
public class AdminController {

	@Autowired
	private UserService userService;

	@Autowired
	private AdminService adminService;

	@Autowired
	private CourseService courseService;

	@Autowired
	private JwtProperties jwtProperties;

	@PostMapping("/login")
	public Result login(@RequestBody UserLoginDto userLoginDto){
		adminService.login(userLoginDto);
		log.info("管理员账号登录：{}", userLoginDto.getUserMobile());
		Admin admin = adminService.login(userLoginDto);
		if (admin == null) {
			return Result.error("管理员用户不存在或密码错误");
		}
		// 为账号登录用户生成JWT令牌
		Map<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.USER_ID, admin.getAdminId());
		String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
		admin.setToken(token); // 设置JWT令牌
		admin.setPassword("");
		log.info("获取管理员信息：{}",admin);
		return Result.success(admin);
	}

	/**
	 * 分页查询用户
	 */
	@GetMapping("/page")
	@ApiOperation("用户分页查询")
	public Result<PageResult> page(UserPageQueryDto userPageQueryDto){
		log.info("用户分页查询：{}",userPageQueryDto);
		PageResult pageResult = userService.pageQuery(userPageQueryDto);
		return Result.success(pageResult);
	}

	/**
	 * 修改用户状态
	 */
	@PostMapping("/status/{status}")
	@ApiOperation("启用禁用用户账号")
	public Result startOrStop(@PathVariable Integer status, Integer id){
		log.info("修改用户:{},状态为{}",status,id);
		return Result.success(userService.startOrStop(status,id));
	}


	/**
	 * 报名或取消报名
	 * @param enrollCourseDto
	 * @return
	 */
	@PostMapping("/addOrdel")
	@ApiOperation("报名课程")
	public Result add(@RequestBody EnrollCourseDto enrollCourseDto){
		log.info("报名课程信息为：{}",enrollCourseDto);
		String res = userService.enrollCourse(enrollCourseDto);
		return Result.success(res);
	}

	@PostMapping("/updateCourse")
	@ApiOperation("修改课程信息")
	public Result updateCourse(@RequestBody Course course){
		SelectCourseVo selectCourseVo = courseService.updateCourse(course);
		return Result.success(selectCourseVo);
	}

	@GetMapping("/getNum")
	@ApiOperation("查看所有报名人数和有效报名人数")
	public Result getNum(@RequestParam(value = "courseId", required = true) Integer courseId){
		//1.显示所有报名的用户信息，包括姓名班级年级校区
		List<UserInfoVo> userInfoVo = courseService.getNumByCourseId(courseId);
		//2.如何筛选有效报名信息
		return Result.success(userInfoVo);
	}

}